package com.springboot.test.exception;

import com.springboot.test.domain.Result;
import com.springboot.test.util.ResultUtil;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @name:
 * @desc:
 * @author: qk on 2017/8/12 18:31.
 */
@ControllerAdvice
public class ExceptionHandle {

    @ExceptionHandler(value = WebException.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof WebException) {
            WebException webException = (WebException) e;
            return ResultUtil.error(webException.getCode(), webException.getMessage());
        }
        else {
            return ResultUtil.error(-1, "内部错误");
        }
    }
}
